package com.qfedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.mapper.TUserMapper;
import com.qfedu.pojo.TUser;
import com.qfedu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 概述:
 *
 * @Author: Tbh
 * @Date: 2022/6/22 15:36
 */

@Service
public class UserServiceImpl extends ServiceImpl<TUserMapper,TUser> implements UserDetailsService, UserService {

    @Autowired
    private PasswordEncoder passwordEncoder;



    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        LambdaQueryWrapper<TUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TUser::getUsername,username);
        TUser tbUser = getOne(lambdaQueryWrapper);
        if (tbUser!=null){
            List<GrantedAuthority> list = new ArrayList<>();
            return new User(username, tbUser.getPassword(),list);
        }
//        if ("jack".equals(username)) {
//            String encode = passwordEncoder.encode("1234");
//            List<GrantedAuthority> list = new ArrayList<>();
//            return new User(username,encode,list);
//        }
        System.out.println("用户名不存在...");
        return null;
    }
}
